<template>
  <div v-loading="loading" class="app-container">
    <!--工具栏-->
    <div class="head-container">
      <div v-if="crud.props.searchToggle">
        <!-- 搜索 -->
        <label class="el-form-item-label">姓名</label>
        <el-input v-model="query.name" clearable placeholder="姓名" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <label class="el-form-item-label">小区地址</label>
        <el-input v-model="query.address" clearable placeholder="小区地址" style="width: 185px;" class="filter-item" @keyup.enter.native="crud.toQuery" />
        <rrOperation :crud="crud" />
      </div>
      <!--如果想在工具栏加入更多按钮，可以使用插槽方式， slot = 'left' or 'right'-->
      <crudOperation :permission="permission" />
      <!--表单组件-->
      <el-dialog :close-on-click-modal="false" :before-close="crud.cancelCU" :visible.sync="crud.status.cu > 0" :title="crud.status.title" width="500px">
        <el-form ref="form" :model="form" :rules="rules" size="small" label-width="80px">
          <el-form-item label="姓名">
            <el-input v-model="form.name" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="小区地址">
            <el-input v-model="form.address" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="具体楼栋">
            <el-input v-model="form.buildings" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="预约时间">
            <el-input v-model="form.bookingTime" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="联系电话">
            <el-input v-model="form.phone" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="预约项id">
            <el-input v-model="form.manageId" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="家政服务标题">
            <el-input v-model="form.title" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="状态 1 待分配 2已分配 3已完成 4拒绝 5退回 6撤回">
            <el-input v-model="form.status" style="width: 370px;" />
          </el-form-item>
          <el-form-item label="实际价格">
            <el-input v-model="form.netPrice" style="width: 370px;" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="text" @click="crud.cancelCU">取消</el-button>
          <el-button :loading="crud.status.cu === 2" type="primary" @click="crud.submitCU">确认</el-button>
        </div>
      </el-dialog>
      <!--表格渲染-->
      <el-table ref="table" v-loading="crud.loading" :data="crud.data" size="small" style="width: 100%;" @selection-change="crud.selectionChangeHandler">
        <el-table-column type="selection" width="55" />
        <el-table-column prop="name" label="姓名" width="150"/>
        <el-table-column prop="address" label="小区地址" width="150" />
        <el-table-column prop="buildings" label="具体楼栋" width="150" />
        <el-table-column prop="bookingTime" label="预约时间" width="150" />
        <el-table-column prop="phone" label="联系电话" width="150" />
        <el-table-column prop="title" label="家政服务标题" width="150" />
        <el-table-column prop="unit" label="套餐类型" width="150" />
        <el-table-column prop="repairContent" label="内容描述" width="150"/>
        <el-table-column label="图片" align="center" width="150">
          <template slot-scope="scope">
            <el-button v-if="scope.row.id && scope.row.detailList && scope.row.detailList.length !== 0" size="small" type="info" @click="dialogDetail(scope.row.detailList)">查看图片</el-button>
            <el-dialog
              :append-to-body="true"
              :visible.sync="dialogDetailVisible"
              width="50%"
              :before-close="handleCloseDetail"
            >
              <el-carousel indicator-position="outside" :interval="0" height="400px">
                <el-carousel-item v-for="item in paths" :key="item">
                  <el-image
                    style="width: 100%; height: 100%"
                    :src="item"
                    fit="contain"
                  />
                </el-carousel-item>
              </el-carousel>
            </el-dialog>
          </template>
        </el-table-column>
        <el-table-column prop="payStatus" label="支付状态" width="150">
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span v-if="scope.row.payStatus === 1">未支付</span>
            <span v-else-if="scope.row.payStatus === 2">线上支付</span>
            <span v-else-if="scope.row.payStatus === 3">线下支付</span>
            <span v-else />
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态" width="150">
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span v-if="scope.row.status === 1">待接单</span>
            <span v-else-if="scope.row.status === 2">已接单</span>
            <span v-else-if="scope.row.status === 3">已完成</span>
            <span v-else-if="scope.row.status === 4">拒绝</span>
            <span v-else-if="scope.row.status === 5">退回</span>
            <span v-else-if="scope.row.status === 6">已取消</span>
            <span v-else-if="scope.row.status === 7">已支付</span>
            <span v-else />
          </template>
        </el-table-column>
        <el-table-column prop="netPrice" label="实际价格" width="150" >
          <template slot-scope="scope">
            <!-- 根据不同的值显示不同的内容 -->
            <span style="color: red">{{scope.row.netPrice}}</span>
          </template>
        </el-table-column>
        <el-table-column prop="takeOrdersName" label="接单人" width="150" />
        <el-table-column prop="createTime" label="创建日期" width="150" />
        <el-table-column label="操作" width="200px" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row.status == 1" size="small" type="primary" @click="dialogHandleClick(scope.row.id)">派单</el-button>
            <el-button v-if="(scope.row.payStatus == 2 || scope.row.payStatus == 3) && scope.row.status == 2" size="small" type="success" @click="completeClick(scope.row.id)">完成订单</el-button>
            <!--            <el-button v-if="scope.row.status == 1" size="small" type="warning" @click="refuseClick(scope.row.id)">拒绝</el-button>-->
            <el-button size="small" v-if="scope.row.status == 1" type="danger" @click="returnClick(scope.row.id)">取消订单</el-button>
            <el-button size="small" v-if="(scope.row.payStatus == 2 || scope.row.payStatus == 3) && (scope.row.status == 2 || scope.row.status == 3 || scope.row.status == 5)" type="danger" @click="requestRefund(scope.row.id)">申请退款</el-button>
            <el-button size="small" style="display: none" @click="dialogVisibleById(scope.row.id)">状态</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!--分页组件-->
      <pagination />
      <!-- 输入框对话框 -->
      <el-dialog
        title="输入框"
        :visible.sync="dialogVisible"
        width="30%"
        :before-close="handleClose"
      >
        <!-- 这里可以放置输入内容 -->
        <!--        <el-input v-model="inputValue" placeholder="请输入内容" />-->
        <el-button size="small" type="primary" @click="submitInput(1)">待接单</el-button>
        <el-button size="small" type="success" @click="submitInput(2)">已接单</el-button>
        <el-button size="small" type="warning" @click="submitInput(3)">已完成</el-button>
        <el-button size="small" type="danger" @click="submitInput(4)">拒绝</el-button>
        <el-button size="small" type="danger" @click="submitInput(5)">退回</el-button>
        <el-button size="small" type="danger" @click="submitInput(6)">已取消</el-button>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogVisible = false">取 消</el-button>
          <!--          <el-button type="primary" @click="submitInput">确 定</el-button>-->
        </span>
      </el-dialog>
      <el-dialog
        title="派单"
        :visible.sync="dialogAmountVisible"
        width="30%"
        :before-close="handleClose"
      >
        <el-form ref="form" size="small" label-width="80px">
          <el-form-item label="订单价格">
            <el-input-number v-model="orderAmount" :min="0" :max="999" style="width: 150px;" />
          </el-form-item>
          <el-form-item label="接单员">
            <el-select v-model="selectedOrderTaker" style="width: 300px;" placeholder="请选择接单员">
              <el-option
                v-for="item in orderTakers"
                :key="item.value"
                :label="item.label"
                :value="item.value">
                <span style="float: left">{{ item.label }}</span>
                <span style="float: right; color: #8492a6; font-size: 13px">{{ item.phone }}</span>
              </el-option>
            </el-select>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogAmountVisible = false">取 消</el-button>
          <el-button type="primary" @click="handleClick">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import crudMenageManageOrders from '@/api/system/menageManageOrders'
import CRUD, { presenter, header, form, crud } from '@crud/crud'
import rrOperation from '@crud/RR.operation'
import crudOperation from '@crud/CRUD.operation'
import udOperation from '@crud/UD.operation'
import pagination from '@crud/Pagination'
import { getTakeOrdersUser } from '@/api/system/user'
import refund from '@/api/system/refund'

const defaultForm = { id: null, createTime: null, updateTime: null, createBy: null, updateBy: null, deleted: null, xqId: null, name: null, address: null, buildings: null, bookingTime: null, phone: null, manageId: null, title: null, status: null, netPrice: null }
export default {
  name: 'MenageManageOrders',
  components: { pagination, crudOperation, rrOperation, udOperation },
  mixins: [presenter(), header(), form(defaultForm), crud()],
  cruds() {
    return CRUD({ title: '家政服务订单', url: 'api/menageManageOrders', query: { refundStatus: 4 }, idField: 'id', sort: 'id,desc', crudMethod: { ...crudMenageManageOrders }})
  },
  data() {
    return {
      permission: {
        add: ['admin', 'menageManageOrders:add'],
        edit: ['admin', 'menageManageOrders:edit'],
        del: ['admin', 'menageManageOrders:del']
      },
      rules: {
      },
      queryTypeOptions: [
        { key: 'name', display_name: '姓名' },
        { key: 'address', display_name: '小区地址' }
      ],
      dialogVisible: false, // 控制对话框显示隐藏
      // inputValue: '', // 输入框的值
      loading: false,
      dialogAmountVisible: false,
      currentId: 0,
      orderAmount: 0,
      orderTakers: [],
      selectedOrderTaker: null,
      dialogDetailVisible: false,
      paths: []
    }
  },
  created() {
    // 获取接单人员
    getTakeOrdersUser(8).then(data => {
      if (data && data.data && data.data.records) {
        for (const item of data.data.records) {
          this.orderTakers.push({ 'value': item.userId, 'label': item.nickName, 'phone': item.phone })
        }
      }
    })
  },
  methods: {
    // 关闭对话框前可进行的操作
    handleClose() {
      // 这里可以添加关闭前需要执行的逻辑
      this.orderAmount = 0
      this.selectedOrderTaker = null
      this.dialogAmountVisible = false
    },
    dialogVisibleById(id) {
      this.currentId = id
      this.dialogVisible = true
    },
    submitInput(status) {
      // 调用接口的示例代码
      this.loading = true
      this.dialogVisible = false
      crudMenageManageOrders.dispose(this.currentId, status).then(data => {
        this.loading = false
        this.orderAmount = 0
        this.crud.operationSuccessNotify('分配成功', 'success')
        this.crud.refresh()
      })
    },
    dialogDetail(paths) {
      if (paths && paths.length !== 0) {
        this.paths = []
        for (let i = 0; i < paths.length; i++) {
          this.paths.push(paths[i].path)
        }
        this.dialogDetailVisible = true
      }
    },
    // 关闭详情进行操作
    handleCloseDetail() {
      this.paths = []
      this.dialogDetailVisible = false
    },
    // 接单弹框
    dialogHandleClick(id) {
      this.currentId = id
      this.dialogAmountVisible = true
    },
    // 接单
    handleClick() {
      // 调用接口的示例代码
      this.loading = true
      crudMenageManageOrders.dispose(this.currentId, 2, this.orderAmount, this.selectedOrderTaker).then(data => {
        this.orderAmount = 0
        this.selectedOrderTaker = null
        this.loading = false
        this.dialogAmountVisible = false
        this.crud.operationSuccessNotify('分配成功', 'success')
        this.crud.refresh()
      })
    },
    completeClick(id) {
      // 调用接口的示例代码
      this.loading = true
      crudMenageManageOrders.dispose(id, 3, '', '').then(data => {
        this.loading = false
        this.crud.operationSuccessNotify()
        this.crud.refresh()
      })
    },
    refuseClick(id) {
      // 调用接口的示例代码
      this.loading = true
      crudMenageManageOrders.dispose(id, 4).then(data => {
        this.loading = false
        this.crud.operationSuccessNotify()
        this.crud.refresh()
      })
    },
    returnClick(id) {
      // 调用接口的示例代码
      this.loading = true
      crudMenageManageOrders.dispose(id, 6, '', '').then(data => {
        this.loading = false
        this.crud.operationSuccessNotify()
        this.crud.refresh()
      })
    },
    requestRefund(id) {
      // 调用接口的示例代码
      this.loading = true
      const data = {
        'orderId': id,
        'type': 4
      }
      refund.submitRefund(data).then(data => {
        this.crud.refresh()
        this.loading = false
      })
    },
    // 钩子：在获取表格数据之前执行，false 则代表不获取数据
    [CRUD.HOOK.beforeRefresh]() {
      this.crud.optShow.add = false
      this.crud.optShow.edit = false
      this.crud.optShow.del = false
      return true
    }
  }
}
</script>

<style scoped>

</style>
